
<template>
  <div>

    <el-dialog
      title="添加关联"
      :visible.sync="relationFlag"
      width="40%"
      :before-close="handleClose"
    >
      <el-row style="margin-top: -40px">
        <div><font color="red">#号前面没有数字,代表没有保存</font></div>
        <el-popover
          placement="right"
          width="160"
          v-model="categoryVisible"
        >
          <div>
            <el-cascader
              ref="mycascader"
              :options="myoptions"
              :props="{ expandTrigger: 'hover',value:'cat_id',label:'name'}"
              @change="handleCascaderChange">
            </el-cascader>
          </div>
          <div style="text-align: right; margin-top: 5px">
            <el-button size="mini" type="text" @click="categoryVisible = false">取消</el-button>
            <el-button type="primary" size="mini" @click="addRelation">确定</el-button>
          </div>
          <el-button slot="reference" @click="">新增关联</el-button>
        </el-popover>
      </el-row>
      <el-row style="margin-top: 10px">
        <el-table
          :data="tableData"
        >
          <el-table-column label="#" prop="id" width="50px">
          </el-table-column>
          <el-table-column label="品牌名" prop="brand_name" width="150px">
          </el-table-column>
          <el-table-column label="分类名" prop="catelog_name" width="150px">
          </el-table-column>
          <el-table-column label="操作" >
          </el-table-column>
        </el-table>
      </el-row>
      <el-row style="margin-top: 10px;margin-left: 30%">
        <el-button @click="$emit('saveArr',newtableData);handleClose();">
          保存
        </el-button>
        <el-button>
          取消
        </el-button>
      </el-row>

    </el-dialog>

    <el-row>
      <el-col :span="8"  style="min-width:150px">
        <el-input  placeholder="搜索名称"></el-input>
      </el-col>
      <el-col :span="1" style="min-width: 80px;margin-left: 10px">
        <el-button @click="findAll">查询</el-button>
      </el-col>
      <el-col :span="1" style="min-width: 80px">
        <el-button @click="currentOperator('save')">新增</el-button>
      </el-col>
    </el-row>
    <el-row style="margin-top: 10px">
      <el-table
        :data="tableData"
        border
        :header-cell-style="{'text-align':'center'}"
        style="width: 100%;">

            <el-table-column
              align="center"
              prop="brand_id"
              label="品牌id"
             >
            </el-table-column>

            <el-table-column
              align="center"
              prop="name"
              label="品牌名"
             >
            </el-table-column>

            <el-table-column
              align="center"
              prop="logo"
              label="品牌logo地址"
             >
            </el-table-column>

            <el-table-column
              align="center"
              prop="descript"
              label="介绍"
             >
            </el-table-column>

            <el-table-column
              align="center"
              prop="show_status"
              label="显示状态[0-不显示；1-显示]"
             >
            </el-table-column>

            <el-table-column
              align="center"
              prop="first_letter"
              label="检索首字母"
             >
            </el-table-column>

            <el-table-column
              align="center"
              prop="sort"
              label="排序"
             >
            </el-table-column>

        <el-table-column
          align="center"
          label="操作">
          <template slot-scope="rows">
            <el-button  @click="currentOperator('relationCategory',rows.row)">关联分类</el-button>
            <el-button  @click="currentOperator('update',rows.row)">修改</el-button>
            <el-button @click="currentOperator('delete',rows.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>

    <el-pagination
      align="center"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.page"
      :page-sizes="[1, 5, 10, 20]"
      :page-size="query.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>


    <pmsbrand-save
    :dialogShow="dialogShow"
    :pmsbrandFrom="pmsbrandFrom"
    @handler="handlerMessage"
    >
    </pmsbrand-save>

  </div>
</template>
<script>

import pmsbrandApi from '@/api/product/PmsBrand'
import pmsCategoryApi from '@/api/product/PmsCategory'
import pmsbrandSave from './PmsBrand-save'

export default {
  name: "pmsbrand",
  components:{
    pmsbrandSave
  },
  data() {
     return {
       relationFlag:false,
       dialogShow:false,
       total:0,
       relationTableData:[],
       tableData:[],
       myoptions:[],
       newtableData:[],
       categoryVisible:false,
       tempFrom:{

       },
       pmsbrandFrom:{
       },
       query:{
         page:0,
         size:5,

       } ,
     }
  },
  methods:{
    handlerMessage(code,message){
        if (code=="close"){
           this.dialogShow=false
           //清空form的数据
           this.pmsbrandFrom={}
        }else if(code=="open"){
          this.dialogShow=true
        }else if(code=="save"){
           this.save(message)
        }else if(code=="update"){
          this.update(message)
        }
    },
    currentOperator(code,obj){
      if (code=="save"){
         this.dialogShow=true
      }else if (code=="update"){
        this.findById(obj.brand_id)
      }else if (code=="delete"){
         //删除单个
         this.delete(obj.brand_id)
      }else if (code=="relationCategory"){
         this.relationCategory(obj)
      }

    },
    handleCascaderChange(value){
      this.tempObj ={
        "catelog_id":value[2],
        "brand_name":this.tempFrom.brand_name,
        "brand_id":this.tempFrom.brand_id,
        "catelog_name":this.$refs.mycascader.getCheckedNodes(true)[0].label
      }
      //到时候把新数组给添加进数据库
      this.newtableData.push(this.tempObj)
    },
    addRelation(){
      this.relationTableData.push(this.tempObj)
      this.categoryVisible = false
    },
    search(){

    },
    findAll(){
      pmsbrandApi.findAll(this.query).then(res=>{
        if (res.code==200){
          this.tableData=res.list
          this.total=res.total
        }else {
            this.$message.error(res.message)
        }
      })
    },
    findById(id){
      pmsbrandApi.findById({"brand_id":id}).then(res=>{
        if (res.code==200){
          this.pmsbrandFrom=res.model
          this.dialogShow=true
        }else {
          this.$message.error(res.message)
        }
      })

    },
    save(obj){
      pmsbrandApi.save(obj).then(res=>{
         if (res.code==200){
            this.$message.success(res.message)
            this.findAll()
         }else {
           this.$message.error(res.message)
         }
      })
    },
    update(obj){
      pmsbrandApi.update(obj).then(res=>{
        if (res.code==200){
          this.$message.success(res.message)
          this.findAll()
        }else {
          this.$message.error(res.message)
        }
      })
    },
    delete(ids){
      this.$confirm('是否删除?', '警告!', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        pmsbrandApi.delete(ids).then(res=>{
          if (res.code==200){
            this.$message.success(res.message)
            this.findAll()
          }else {
            this.$message.error(res.message)
          }
        })
      }).catch(() => {
          //取消后的逻辑
      });

    },
    handleSizeChange(size){
       this.query.size=size
       this.findAll()
    },
    handleCurrentChange(page){
      this.query.page=page
      this.findAll()
    },
    handleClose(){
      this.relationFlag=false
    },
    relationCategory(obj){
        this.relationFlag=true
        this.tempFrom=obj
    //   通过品牌id查询所有关联的分类

    },
  },
  created() {
    this.findAll()
    //级联的分类数据
    pmsCategoryApi.findTree().then(res=>{
        this.myoptions=res.list
    })
  }
}
</script>

<style lang="less" scoped>
</style>

